package com.ruoyi.api.controller.user;


import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.business.domain.dto.api.user.AddMyAddressDTO;
import com.ruoyi.business.domain.dto.api.user.EditDefaultAddressDTO;
import com.ruoyi.business.domain.dto.api.user.EditMyAddressDTO;
import com.ruoyi.business.domain.koc.TbUserAddress;
import com.ruoyi.business.service.koc.ITbUserAddressService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;

@Validated
@Api(value = "小程序用户收货管理控制器", tags = {"小程序用户收获管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/miniapp/user/address")
public class ApiUserAddressController extends AbstractApiController {

    private final ITbUserAddressService iTbUserAddressService;


    /**
     * 查询用户收货地址列表
     */
    @ApiOperation("查询我的收货地址列表")
    @GetMapping("/page-list")
    public TableDataInfo<TbUserAddress> queryMyAddressPage(PageQuery pageQuery) {
        return iTbUserAddressService.queryMyAddressPageList(getUserId(), pageQuery);
    }

    /**
     * 删除用户收货地址
     */
    @ApiOperation("删除用户收货地址")
    @DeleteMapping("/del/{ids}")
    public R<Boolean> remove(@ApiParam("主键串")
                             @NotEmpty(message = "主键不能为空")
                             @PathVariable String[] ids) {
        return R.ok(iTbUserAddressService.deleteByIds(Arrays.asList(ids)));
    }

    /**
     * 新增用户收货地址
     */
    @ApiOperation("新增我的收货地址")
    //@RepeatSubmit(interval = 500, timeUnit = TimeUnit.MILLISECONDS, message = "不允许重复提交")
    @PostMapping("/add")
    public R<Boolean> add(@Validated @RequestBody AddMyAddressDTO dto) {
        return R.ok(iTbUserAddressService.insertMyAddress(getUserId(), dto));
    }

    /**
     * 修改用户收货地址
     */
    @ApiOperation("修改我的收货地址")
    @PutMapping("/edit")
    public R<Boolean> edit(@Validated @RequestBody EditMyAddressDTO dto) {
        return R.ok(iTbUserAddressService.editMyAddress(getUserId(), dto));
    }

    /**
     * 设置默认地址
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default-address")
    public R<Boolean> setDefaultAddress(@Validated @RequestBody EditDefaultAddressDTO dto) {
        return R.ok(iTbUserAddressService.setMyDefaultAddress(getUserId(), dto.getId()));
    }


}
